package com.atguigu.self.gulimall.product.service.impl;

import com.atguigu.self.gulimall.product.entity.AttrEntity;
import com.atguigu.self.gulimall.product.service.AttrService;
import com.atguigu.self.gulimall.product.vo.BaseAttrs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.self.common.utils.PageUtils;
import com.atguigu.self.common.utils.Query;

import com.atguigu.self.gulimall.product.dao.ProductAttrValueDao;
import com.atguigu.self.gulimall.product.entity.ProductAttrValueEntity;
import com.atguigu.self.gulimall.product.service.ProductAttrValueService;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.Null;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveProductAttrValue(Long id, List<BaseAttrs> baseAttrs) {
        if (baseAttrs != null && baseAttrs.size() > 0) {
            List<ProductAttrValueEntity> attrValueEntities = baseAttrs.stream().map(attr -> {
                ProductAttrValueEntity productAttrValueEntity = new ProductAttrValueEntity();
                productAttrValueEntity.setSpuId(id);
                productAttrValueEntity.setAttrId(attr.getAttrId());
                productAttrValueEntity.setAttrValue(attr.getAttrValues());
                productAttrValueEntity.setQuickShow(attr.getShowDesc());
                // 保存基本属性名称需要在基本属性表中查询到
                AttrEntity attrEntity = attrService.getById(attr.getAttrId());
                productAttrValueEntity.setAttrName(attrEntity.getAttrName());

                return productAttrValueEntity;
            }).collect(Collectors.toList());
            this.saveBatch(attrValueEntities);
        }

    }

    @Override
    public List<ProductAttrValueEntity> baseListforspu(Long spuId) {
        List<ProductAttrValueEntity> attrValueEntities = this.list(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));

        return attrValueEntities;
    }

    @Transactional
    @Override
    public void updateSpuAttr(Long spuId, List<ProductAttrValueEntity> entities) {

        // 更新规格参数时， 前端已经将此spu下的所有规格参数传过来了， 那么只需要将数据库表中对应的spu规格参数删除重新新增即可
        // 1、删除spu对应所有规格参数
        this.baseMapper.delete(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));

        // 2、重新插入
        // 为规格参数(基本属性)设置所属的spuId
        List<ProductAttrValueEntity> productAttrValueEntities = entities.stream().map(item -> {
            item.setSpuId(spuId);
            return item;
        }).collect(Collectors.toList());
        this.saveBatch(productAttrValueEntities); // 批量保存
    }

}